package com.smile.frame.common.result;

import com.smile.frame.common.exception.BaseErrorCodeEnum;
import com.smile.frame.common.exception.ErrorCodeEnumInterface;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 响应对象
 *
 * @author LGC
 */
@ApiModel(description = "响应对象")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result<T> implements Serializable {

    private static final long serialVersionUID = -1455858674704802637L;
    /**
     * 状态码
     */
    @ApiModelProperty("状态码")
    private Integer code;
    /**
     * 提示信息
     */
    @ApiModelProperty("提示信息")
    private String msg;
    /**
     * 具体对象信息
     */
    @ApiModelProperty("具体对象信息")
    private T data;


    /**
     * 无数据对象成功返回
     *
     * @param <T>
     * @return
     */
    public static <T> Result<T> ok() {
        return Result.ok(null);
    }

    /**
     * 有数据对象成功返回
     *
     * @param t
     * @param <T>
     * @return
     */
    public static <T> Result<T> ok(T t) {
        Result<T> result = new Result<>();
        result.setCode(BaseErrorCodeEnum.BASE_SUCCESS.getCode());
        result.setMsg(BaseErrorCodeEnum.BASE_SUCCESS.getMsg());
        result.setData(t);
        return result;
    }

    /**
     * 失败返回结果
     *
     * @param errorCodeEnumInterface 错误码
     */
    public static <T> Result<T> error(ErrorCodeEnumInterface errorCodeEnumInterface) {
        return new Result<>(errorCodeEnumInterface.getCode(), errorCodeEnumInterface.getMsg(), null);
    }

    /**
     * 失败返回结果
     *
     * @param errorCodeEnumInterface 错误码
     * @param message                错误信息
     */
    public static <T> Result<T> error(ErrorCodeEnumInterface errorCodeEnumInterface, String message) {
        return new Result<>(errorCodeEnumInterface.getCode(), errorCodeEnumInterface.getMsg() + message, null);
    }

    /**
     * 失败返回结果
     *
     * @param message 错误信息
     */
    public static <T> Result<T> error(String message) {
        return new Result<T>(BaseErrorCodeEnum.BASE_FAIL.getCode(), message, null);
    }

}
